home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 001-025 / scopedisk14 / c64em / screen.c < prev    next >
C/C++ Source or Header  |  1995-03-18  |  8KB  |  388 lines

  1. /*
  2.  *        Commodore 64 Spoof Emulator (C) Eddy Carroll, 1st April 1988
  3.  *
  4.  * Module: SCREEN.C
  5.  *
  6.  * This module contains the various screen handling routines used by
  7.  * the emulator.
  8.  *
  9.  */
  10.  
  11. #define  SCREEN
  12.  
  13. #include <exec/types.h>
  14. #include <exec/io.h>
  15. #include <intuition/intuition.h>
  16. #include "screen.h"
  17.  
  18. struct RastPort *rp;
  19. int openconsole = 0;
  20.  
  21. struct NewScreen NewScreen = {
  22.     0,0,320,200,    /* Screen position & size     */
  23.     2,                /* 2 bitplanes = 4 colours     */
  24.     0,1,            /* Default text colours        */
  25.     0,                /* Lores, non-interlace        */
  26.     CUSTOMSCREEN,    /* Screen type                */
  27.     0,                /* Use default font for now    */
  28.     "C64 Emulator",    /* Screen title                */
  29.     0,                /* No gadgets                 */
  30.     0                /* No custom bitmap            */
  31. };
  32.  
  33. #define NORMALFLAGS WINDOWCLOSE | WINDOWSIZING | WINDOWDEPTH | WINDOWDRAG
  34. struct NewWindow NewWindow = {
  35.     0,0,320,200,      /* Window position & Size    */
  36.     0,1,            /* Default text colours        */
  37.     MENUPICK      |    /* Ok, we want to know         */
  38.     INTUITICKS,        /* about menus & timings     */
  39.     BACKDROP      |    /* These four flags define    */
  40.     BORDERLESS    |    /* the type of window we    */
  41.     ACTIVATE      |    /* open. Here, we want an    */
  42.     0       ,        /* "invisible" window.        */
  43.     NULL,            /* No gadgets                */
  44.     NULL,            /* Default menus check mark    */
  45.     NULL,              /* No window title            */
  46.     NULL,            /* Will point to our screen    */
  47.     NULL,            /* No special bitmap used    */
  48.     0,0,320,200,    /* No resizing so not used  */
  49.     CUSTOMSCREEN     /* Open this on OUR screen    */
  50. };
  51.  
  52. UWORD blackcol[4] = {    /* All black array for initial screen   */
  53.     0,0,0,0};
  54.  
  55. UWORD screencol[4] = {    /* Default colours for custom screen    */
  56.     0x077E,                /* Colour 0 = Light blue                */
  57.     0x0EEE,                /* Colour 1 = White                        */
  58.     0x077E,                /* Colour 2 = Lighter blue                */
  59.     0x011C                 /* Colour 3 = Dark Blue                    */
  60. };
  61.  
  62. struct IntuiText mytext[] = {
  63. /*  Pens   Mode   Pos   Font    Text                       Next */
  64. {   0,1,   JAM1,  0,0,  NULL,   (UBYTE *)" About...",       NULL},
  65. {   0,1,   JAM1,  0,0,  NULL,   (UBYTE *)" Show Title",     NULL},
  66. {   0,1,   JAM1,  0,0,  NULL,   (UBYTE *)" Quit",           NULL}};
  67.  
  68.  
  69.  
  70. #define MENUFLAGS ITEMTEXT | ITEMENABLED | HIGHCOMP
  71. #define IDATA(off,str,key) 0,off,110,9,MENUFLAGS,0,str,NULL,key,NULL,NULL
  72.  
  73. struct MenuItem myitems[] = {
  74. { &myitems[1],  IDATA( 0, (APTR)&mytext[0],  M_ABOUT)  },
  75. { &myitems[2],  IDATA(10, (APTR)&mytext[1],  M_TITLE)  },
  76. { NULL,         IDATA(20, (APTR)&mytext[2],  M_QUIT )  }};
  77.  
  78. struct Menu mymenus = {NULL, 0,0,100,10, MENUENABLED, "Project", myitems};
  79.  
  80.  
  81. /* 
  82.  * Free all resources allocated in this module
  83.  *
  84.  */
  85.  
  86. void cleanup(err)
  87. int err;
  88. {
  89.     if (openconsole)
  90.         CloseDevice(ConReadReq);
  91.  
  92.     if (ConReadReq)
  93.         DeleteStdIO(ConReadReq);
  94.  
  95.     if (ConReadPort)
  96.         DeletePort(ConReadPort);
  97.  
  98.     if (mywin) {
  99.         ClearMenuStrip(mywin);
  100.         CloseWindow(mywin);
  101.     }
  102.     if (myscreen)
  103.         CloseScreen(myscreen);
  104.  
  105.     if (IntuitionBase)
  106.         CloseLibrary(IntuitionBase);
  107.  
  108.     if (GfxBase)
  109.         CloseLibrary(GfxBase);
  110.  
  111.     exit(err);
  112. }
  113.  
  114.  
  115.  
  116. /* 
  117.  * Clears the screen
  118.  *
  119.  */
  120.  
  121. void clearscreen()
  122. {
  123.     register char *p;
  124.     register int i;
  125.     SetRast(rp,3);
  126.     for (p = (char *)screen, i = 1000; i--; *p++ = ' ')
  127.         ;
  128.     cursorx = 0;
  129.     cursory = 0;
  130. }
  131.  
  132.  
  133. /* 
  134.  * Tells console device we want to read a character from keyboard
  135.  *
  136.  */
  137.  
  138. void QueueRead(request,whereto)
  139. struct IOStdReq *request;
  140. char *whereto;
  141. {
  142.     request->io_Command    = CMD_READ;
  143.     request->io_Data    = (APTR)whereto;
  144.     request->io_Length    = 1;
  145.     SendIO(request);
  146. }
  147.  
  148. /*  
  149.  * Initialises the screen, character set, colours etc. Exits program with
  150.  * error code if an error occurs.
  151.  *
  152.  */
  153.  
  154. void initscreen()
  155. {
  156.     if (!mywin) {
  157.         if ((IntuitionBase = 
  158.             (struct IntuitionBase *)OpenLibrary("intuition.library",0)) == 0)
  159.             cleanup(1);
  160.  
  161.         if ((GfxBase =
  162.             (struct GfxBase *)OpenLibrary("graphics.library",0)) == 0)
  163.             cleanup(2);
  164.  
  165.         if ((myscreen = (struct Screen *)OpenScreen(&NewScreen)) == 0)
  166.             cleanup(3);
  167.  
  168.         LoadRGB4(&(myscreen->ViewPort),blackcol,4); 
  169.         ShowTitle(myscreen,0);
  170.         NewWindow.Screen = myscreen;
  171.  
  172.         if ((mywin =  (struct Window *)OpenWindow(&NewWindow)) == 0)
  173.             cleanup(4);
  174.  
  175.         SetMenuStrip(mywin, &mymenus);
  176.         rp = mywin->RPort;
  177.  
  178.         /* Initialise communication ports for Console */
  179.  
  180.         if ((ConReadPort = CreatePort(0,0)) == NULL)
  181.             cleanup(5);
  182.  
  183.         if ((ConReadReq = CreateStdIO(ConReadPort)) == NULL)
  184.             cleanup(6);
  185.  
  186.         ConReadReq->io_Data = (APTR)mywin;
  187.         ConReadReq->io_Length = sizeof(*mywin);
  188.  
  189.         /* Open console and tell it to turn off the cursor */
  190.         if (OpenDevice("console.device",0,ConReadReq,0))
  191.             cleanup(7);
  192.         openconsole = 1;
  193.  
  194.         /* Tell console to get first character ready for us */
  195.         QueueRead(ConReadReq,constring);
  196.  
  197.         SetAPen(rp,2);    /* Set primary colour */
  198.         SetBPen(rp,3);    /* Set secondary colour */
  199.         clearscreen();    /* Fill screen to raster colour */
  200.         LoadRGB4(&(myscreen->ViewPort),screencol,4); 
  201.  
  202.     }
  203. }
  204.  
  205.  
  206. /*
  207.  * Outputs a character to the screen at co-ordinates x,y. If mode is 0,
  208.  * then output normal character, else output reverse character.
  209.  *
  210.  */
  211.  
  212. void writechar(x,y,ch,mode)
  213. int x,y;
  214. char ch;
  215. int mode;
  216. {
  217.     char s[2];
  218.     s[0] = ch;
  219.     Move(rp,x * 8, y * 8 + 6); /* Quick multiply by 8 */
  220.     if (mode) {
  221.         SetDrMd(rp,INVERSVID|JAM2);
  222.         Text(rp,s,1);
  223.         SetDrMd(rp,          JAM2);
  224.     } else
  225.         Text(rp,s,1);
  226. }
  227.  
  228. /* 
  229.  * Scrolls the screen up one line. Both the visible rastport screen, 
  230.  * and the internal screen are scrolled.
  231.  *
  232.  */
  233.  
  234. void scrollup()
  235. {
  236.     register char *p, *q;
  237.     register int i;
  238.  
  239.     ScrollRaster(rp,0,8,0,0,319,199);
  240.     for (p = (char *)screen,q = (char *)screen+40, i = 960; i--; *p++ = *q++)
  241.         ;
  242.     for (i = 40; i--; *p++ = ' ')
  243.         ;
  244.     cursory--;
  245.  
  246. }
  247.  
  248.  
  249.  
  250. /*
  251.  * Handles special characters like CR, cursor keys etc.
  252.  *
  253.  */
  254.  
  255. void special(ch)
  256. char ch;
  257. {
  258.     register char *p, *q;
  259.     register int i,j;
  260.  
  261.     switch (ch) {
  262.  
  263.     case C_CR: /* Carriage Return */
  264.         cursorx = 0;
  265.         cursory++;
  266.         if (cursory >= 25)
  267.             scrollup();
  268.         break;
  269.  
  270.     case C_UP: /* Cursor Up */
  271.         if (cursory)
  272.             cursory--;
  273.         break;
  274.  
  275.     case C_DOWN: /* Cursor Down */
  276.         cursory++;
  277.         if (cursory >= 25)
  278.             scrollup();
  279.         break;
  280.  
  281.     case C_LEFT: /* Cursor Left */
  282.         if (cursorx == 0) {
  283.             if (cursory) {
  284.                 cursorx = 39;
  285.                 cursory--;
  286.             }
  287.         } else
  288.             cursorx--;
  289.         break;
  290.  
  291.     case C_RIGHT: /* Cursor Right */
  292.         cursorx++;
  293.         if (cursorx >= 40) {
  294.             cursorx = 0;
  295.             cursory++;
  296.             if (cursory >= 25)
  297.                 scrollup();
  298.         }
  299.         break;
  300.  
  301.     case C_DEL: /* Delete */
  302.         if (cursorx == 0) {
  303.             if (cursory) {
  304.                 cursorx = 39;
  305.                 cursory--;
  306.                 screen[cursory][cursorx] = ' ';
  307.                 writechar(cursorx,cursory,' ',0);
  308.             }
  309.         } else {
  310.             ScrollRaster(rp,8,0,cursorx*8,cursory*8,319,cursory*8+7);
  311.             /* Move chars from here to end of line back one space */
  312.             for (q = &(screen[cursory][cursorx]),
  313.                         p = q - 1, i = 40 - cursorx;
  314.                         i--;
  315.                         *p++ = *q++)
  316.                         ;
  317.             *p = ' ';
  318.             cursorx--;
  319.         }
  320.         break;
  321.  
  322.     case C_INSERT: /* Insert */
  323.         if (screen[cursory][39] == ' ' && cursorx < 39) {
  324.             ScrollRaster(rp,-8,0,cursorx*8,cursory*8,319,cursory*8+7);
  325.             for (p = &screen[cursory][39], q = p - 1, i = 39 - cursorx;
  326.                     i--; *p-- = *q--)
  327.                         ;
  328.             *p = ' ';
  329.         }
  330.         break;
  331.  
  332.     case C_CLEAR: /* Clear screen */
  333.         clearscreen();
  334.         break;
  335.  
  336.     case C_HOME: /* Home cursor */
  337.         cursorx = 0;
  338.         cursory = 0;
  339.         break;
  340.  
  341.     case C_REDRAW: /* Redraw entire screen */
  342.         for (i = 0; i < 25; i++)
  343.             for (j = 0; j < 40; j++)
  344.                 writechar(j,i,screen[i][j],0);
  345.         break;
  346.  
  347.     }
  348. }
  349.  
  350.  
  351.  
  352. /*
  353.  * Outputs character to screen at current cursor position, and advances
  354.  * cursor to next position. Scrolls screen if necessary.
  355.  *
  356.  */
  357.  
  358. void printchar(ch)
  359. char ch;
  360. {
  361.     if (ch < ' ')
  362.         special(ch);
  363.     else {
  364.         writechar(cursorx,cursory,ch,0);
  365.         screen[cursory][cursorx] = ch;
  366.         cursorx++;
  367.         if (cursorx >= 40) {
  368.             cursorx = 0;
  369.             cursory++;
  370.             if (cursory >= 25)
  371.                 scrollup();
  372.         }
  373.     }
  374. }
  375.  
  376.  
  377. /* 
  378.  * Outputs string to screen at current cursor position
  379.  *
  380.  */
  381.  
  382. void printmess(s)
  383. char *s;
  384. {
  385.     while (*s)
  386.         printchar(*s++);
  387. }
  388.